<?php
/**
 * PlaceholderController
 * 
 * @author Andrey Mukhin
 * @version 2.0
 */


require_once 'Zend/Controller/Action.php';


class Cms_PlaceholderController extends Zend_Controller_Action {
	
	
	public function indexAction() {
		CmsAuth::check();
		if (NULL === $id = $this->getRequest()->getParam('id')) {
			throw new Zend_Controller_Action_Exception('Required param "Id"', 404);
		}
		
		$phItem  = MF_Main::getInstance()->Placeholder->getById($id);
		
		if (NULL !== $act = $this->getRequest()->getParam('act')) {
			
			if (NULL === $cid = $this->getRequest()->getParam('cid')) {
				throw new Zend_Controller_Action_Exception('Required param "Cid" in case "Act" param is set', 404);
			}
			
			$phContentItem = $phItem->getById($cid);
			
			switch ($act) {
				case 'up':
					$phContentItem->sequence = $phContentItem->sequence - 1;
					break;
				
				case 'down':
					$phContentItem->sequence = $phContentItem->sequence + 1;
					break;
					
				case 'del':
					$phContentItem->delete();
					break;
				
				default:
					throw new Zend_Controller_Action_Exception('Invalid value for optional param "Act"', 404);
					break;
			}
			
		}
		
		
		$this->view->content_list = $phItem;
		$this->_helper->layout->disableLayout();
	}
	
	
	
	public function addAction() {
		CmsAuth::check();
		if (NULL === $id = $this->getRequest()->getParam('id')) {
			throw new Zend_Controller_Action_Exception('Required param "Id"', 404);
		}
		
		$mfMain = MF_Main::getInstance();
		$placeholderItem = $mfMain->Placeholder->getById($id);
		
		
		
		if ($this->_request->isPost()) {
			$post_data = $this->_request->getPost();
			$post_name = trim($post_data['name']);
			$post_type = $post_data['type'];
			$errors = array();
			if (strlen($post_name)==0) {
				$errors['name'] = array('Название не может быть пустым');
			}
			if ($post_type=='') {
				$errors['type'] = array('Необходимо выбрать тип');
			}
			if (!$placeholderItem->checkType($post_type)) {
				$errors['type'] = array('Неверный тип');
			}
			
			
			if (count($errors)==0) {
				$placeholderItem->add($post_type)->name = $post_name;
				$this->render('exit');
			} else {
				$this->view->values = $post_data;
				$this->view->errors = $errors;
			}
			
			
		} 
		
		$avalible_content = array();
		foreach ($placeholderItem->getTypes() as $type) {
			array_push($avalible_content,array($type,$mfMain->Content($type)->content_name));
		}
		$this->view->avalible_content = $avalible_content;
		
	}
	

	public function geoAction() {
		CmsAuth::check();
		if (NULL === $ph_id = $this->getRequest()->getParam('ph_id')) {
			throw new Zend_Controller_Action_Exception('Required param "Ph_Id"', 404);
		}
		if (NULL === $content_id = $this->getRequest()->getParam('content_id')) {
			throw new Zend_Controller_Action_Exception('Required param "Contend_Id"', 404);
		}
		
		
		$main = MF_Main::getInstance();
		$geoObj = $main->Geo;
		$content = $main->Placeholder->getById($ph_id)->getById($content_id);
		
		if ($this->_request->isPost()) {
			$post_data = $this->_request->getPost();
			
			$checked_vals = isset($post_data['geo']) ? $post_data['geo'] : array();
			for ($g=0;$g<$geoObj->length();$g++) {
				if (in_array($geoObj->item($g)->_content_id,$checked_vals)) {
					if (!$content->checkGeo($geoObj->item($g))) {
						$content->linkGeo($geoObj->item($g));
					}
				} else {
					if ($content->checkGeo($geoObj->item($g))) {
						$content->unlinkGeo($geoObj->item($g));
					}
				}
				
			}
			
			$this->render('exit');
			return true;
			
		}
		
		$this->view->geoList = $geoObj;
		$this->view->phContent = $content;
	}
	
	

	
	
}

?>